#!/bin/bash
options=$(getopt -o n:a:g:p --long name:,age:,gender:,print -- "$@")
eval set -- "$options"

# 提取选项和参数
while true; do
  case $1 in
  	-a | --age) shift; age=$1 ; shift ;;
    -n | --name) shift; name=$1 ; shift ;;
    -g | --gender) shift; gender=$1 ; shift ;;
    -p | --print) print=true; shift ;;
    --) shift ; break ;;
    *) echo "Invalid option: $1" exit 1 ;;
  esac
done

# 检查变量
if [ -z "$age" ]; then
    echo "Error: age is required"
    exit 1
fi
if [ -z "$name" ]; then
    echo "Error: name is required"
    exit 1
fi
if [ -z "$gender" ]; then
    echo "Error: gender is required"
    exit 1
fi

# 判断开关选项
if [ "$print" = true ]; then
    echo "NameVal: $name; AgeVal: $age; GenderVal: $gender";
fi

:<<EOF

bash userReg.sh  --name Kite --age 18 --gender f -p
bash userReg.sh   -n Kite -a 18 -g f -p
EOF